
var gameInfo = require("gameStrategy");
var GameState = require("gameState");

cc.Class({
  extends: cc.Component,

  properties: {
    roadblockOne: {
      default: null,
      type: cc.Prefab
    },
    roadblockTwo: {
      default: null,
      type: cc.Prefab
    },
    roadblockThree: {
      default: null,
      type: cc.Prefab
    },
    background: {
      default: null,
      type: cc.Node
    },
  },

  // use this for initialization
  onLoad: function () {
    var screen = cc.view.getVisibleSize();
    this.screenWidth = screen.width;
    this.screenHeight = screen.height;

    this.roadblocks = new Map();
    this.roadblockIndex = 0;
    this.speed = 0;
    this.posXArray = new Array();
    this.wayNumber = gameInfo.gameRunInfo.wayNumber;
    this.wayWidth = gameInfo.gameRunInfo.wayWidth;
    var startPosX = -((this.wayNumber - 1) * this.wayWidth / 2);
    this.nextTime = 0;
    this.intervalTime = 0;
    for (var i = 0; i < this.wayNumber; i++) {
      this.posXArray[i] = i * this.wayWidth + startPosX;
    }

    this.roadblockOne.active = false;
    this.roadblockTwo.active = false;
    this.roadblockThree.active = false;

    this.createRoadblocksCache();
  },

  createRoadblocksCache: function () {
    var cacheNum = 0;
    this.roadblocksOneCache = new Array();
    this.roadblocksTwoCache = new Array();
    this.roadLoversCache = new Array();
    this.roadblocksThreeCache = new Array();
    for (var i = 0; i < cacheNum; i++) {
      this.createRoadblock1();
      this.createRoadblock2();
      this.createLovers();
      this.createRoadblock3();
    }
    
    this.clearRoadblocks();
  },

  // called every frame
  update: function (dt) {
    if (this.state != GameState.RUNNING) {
      return;
    }

    this.intervalTime += dt;
    if (this.nextTime > 0 && this.nextTime < this.intervalTime) {
      this.intervalTime = 0;
      this.randomTime();
      this.createRoadblock();
    }
  },

  randomTime: function () {
    var randomValue = Math.random();
    var value = (this.maxTime - this.minTime) * randomValue;
    this.nextTime = this.minTime + value;
  },

  createRoadblock: function () {
    //计算下一个路障的位置，随机左中右
    var randomValue = Math.random();
    var posIndex = Math.floor(randomValue * this.wayNumber);
    if (posIndex >= this.wayNumber || posIndex < 0) {
      cc.log("create roadblock find random result result. posIndex: " + posIndex);
      posIndex = 0;
    }
    var posX = this.posXArray[posIndex];
    var posY = this.screenHeight / 2 + 100;

    //根据配置，随机
    var randomValue = Math.random();
    var value = 100 * randomValue;

    //情侣
    if (value < gameInfo.gameRunInfo.probabilityTwoTrue) {
      this.createLovers(posX, posY);
      return;
    }

    //两人障碍快（同性别）
    value -= gameInfo.gameRunInfo.probabilityTwoTrue;
    if (value < gameInfo.gameRunInfo.probabilityTwoError) {
      this.createRoadblock2(posX, posY);
      return;
    }

    //一人障碍快
    value -= gameInfo.gameRunInfo.probabilityTwoError;
    if (value < gameInfo.gameRunInfo.probabilityOne) {
      this.createRoadblock1(posX, posY);
      return;
    }

    //三人障碍快
    this.createRoadblock3(posX, posY);
    return;
  },

  getNextRoadblockIndex: function () {
    return this.roadblockIndex++;;
  },

  randomNum: function () {
    var randomValue1 = Math.random();
    var result = Math.ceil(randomValue1 * gameInfo.gameRunInfo.manNumber);
    if (result <= 0 || result >= gameInfo.gameRunInfo.manNumber) { result = gameInfo.gameRunInfo.manNumber; }
    return result;
  },

  // 开始
  running: function (speed) {
    this.state = GameState.RUNNING;
    this.speed = speed;
    this.clearRoadblocks();
  },

  clearRoadblocks: function(){
    var roadblockManagerObj = this;
    this.roadblocks.forEach(
      function (value, key, map) {
        // var rb = value.getComponent("roadblock");
        roadblockManagerObj.removeRoadblock(value);
        // value.destroy();
      });
    this.roadblocks.clear();
  },

  // 暂停
  pause: function () {
    this.state = GameState.PAUSE;

    this.roadblocks.forEach(
      function (value, key, map) {
        var rb = value.getComponent("roadblock");
        rb.pause();
      });
  },

  // 恢复
  continue: function () {
    this.state = GameState.RUNNING;

    this.roadblocks.forEach(
      function (value, key, map) {
        var rb = value.getComponent("roadblock");
        rb.continue();
      });
  },

  // 停止
  stop: function () {
    this.state = GameState.STOP;
    this.roadblocks.forEach(
      function (value, key, map) {
        var rb = value.getComponent("roadblock");
        rb.stop();
      });
  },

  // 设置速度
  setSpeed: function (speed) {
    cc.log("roadblockmanager setSpeed : " + speed);
    this.speed = speed;

    this.roadblocks.forEach(
      function (value, key, map) {
        var rb = value.getComponent("roadblock");
        rb.setSpeed(speed);
      });
  },

  // 设置阶段
  setStageInfo: function (stageInfo) {
    this.stageInfo = stageInfo;
    this.minTime = parseFloat(this.stageInfo["minTime"]);
    this.maxTime = parseFloat(this.stageInfo["maxTime"]);
    this.randomTime();
  },

  removeRoadblock: function (obj) {
    if (obj == null) {
      cc.error("removeRoadblock find roadblock prefab is null");
      return;
    }

    var name = obj.name;
    cc.log("---> 13 remove roadblock name : " + name);
    var rb = obj.getComponent("roadblock");
    var peopleNum = rb.getpeopleNum();
    var isBlock = rb.isRoadBlock();
    if (peopleNum == 2 && isBlock) {
      this.roadblocksTwoCache.push(obj);
    } else if (peopleNum == 2) {
      this.roadLoversCache.push(obj);
    } else if (peopleNum == 3) {
      this.roadblocksThreeCache.push(obj);
    } else if (peopleNum == 1) {
      this.roadblocksOneCache.push(obj);
    }
    rb.hide();
    // obj.destroy();
    this.roadblocks.delete(name);
  },

  // getRoadblockOneCache: function () {
  //   var roadblock = this.roadblocksOneCache.pop();
  //   if (roadblock == undefined) {
  //     // cc.error("生成新的单个障碍块");
  //     roadblock = cc.instantiate(this.roadblockOne);
  //     this.roadblocksOneCache.push(roadblock);
  //   }
  //   return roadblock;
  // },

  //创建一个人的障碍快
  createRoadblock1: function (posX, posY) {       
    var score = gameInfo.gameRunInfo.baseScoreChange;
    var magicScore = gameInfo.gameRunInfo.baseMagicChange;
    var name = null;  
    var rb = null;    

    var roadblock = this.roadblocksOneCache.pop();
    if (roadblock == undefined) {
      // cc.error("生成新的双人障碍块");
      roadblock = cc.instantiate(this.roadblockOne);      
      var leftSpriteName = null, middleSpriteName = null, rightSpriteName = null;

      var randomTmp = Math.random();
      if (randomTmp < 0.5) {
        leftSpriteName = "nan" + this.randomNum();
      } else {
        leftSpriteName = "nv" + this.randomNum();
      }
  
      name = "roadblock1+" + this.getNextRoadblockIndex();  
      roadblock.name = name;
      roadblock.parent = this.node;
      
      rb = roadblock.getComponent("roadblock");
      rb.initRoadblock(this, leftSpriteName, middleSpriteName, rightSpriteName, true, 1);
    }
    else
    {
      rb = roadblock.getComponent("roadblock");
      name = roadblock.name;  
    }

    roadblock.setPosition(cc.p(posX, posY));

    rb.resetRoadblockData(score, magicScore, this.speed);  
    rb.running(this.speed);  
    rb.show();

    this.addRoadblocks(name, roadblock);//this.roadblocks.set(name, roadblock);
    return roadblock;
  },

  //创建两个人的障碍快
  createRoadblock2: function (posX, posY) {       
    var score = gameInfo.gameRunInfo.baseScoreChange;
    var magicScore = gameInfo.gameRunInfo.baseMagicChange;
    var name = null; 
    var rb = null;     

    var roadblock = this.roadblocksTwoCache.pop();
    if (roadblock == undefined) {
      // cc.error("生成新的双人障碍块");
      roadblock = cc.instantiate(this.roadblockTwo);      
      var leftSpriteName = null, middleSpriteName = null, rightSpriteName = null;
      var randomTmp = Math.random();
      if (randomTmp < 0.5) {
        leftSpriteName = "nan" + this.randomNum();
        rightSpriteName = "nan" + this.randomNum();
      } else {
        leftSpriteName = "nv" + this.randomNum();
        rightSpriteName = "nv" + this.randomNum();
      }   
  
      name = "roadblock2+" + this.getNextRoadblockIndex();  
      roadblock.name = name;
      roadblock.parent = this.node;
      
      rb = roadblock.getComponent("roadblock");
      rb.initRoadblock(this, leftSpriteName, middleSpriteName, rightSpriteName, true, 2);
    }
    else
    {
      rb = roadblock.getComponent("roadblock");
      name = roadblock.name;  
    }

    roadblock.setPosition(cc.p(posX, posY));

    rb.resetRoadblockData(score, magicScore, this.speed);  
    rb.running(this.speed); 
    rb.show(); 

    this.addRoadblocks(name, roadblock);//this.roadblocks.set(name, roadblock);
    return roadblock;
  },

  //创建情侣
  createLovers: function (posX, posY) {       
    var score = gameInfo.gameRunInfo.baseScoreChange;
    var magicScore = gameInfo.gameRunInfo.baseMagicChange;
    var name = null;    
    var rb = null;  

    var roadblock = this.roadLoversCache.pop();
    if (roadblock == undefined) {
      // cc.error("生成新的双人障碍块");
      roadblock = cc.instantiate(this.roadblockTwo);      
      var leftSpriteName = null, middleSpriteName = null, rightSpriteName = null;
      var randomTmp = Math.random();
      if (randomTmp < 0.5) {
        leftSpriteName = "nan" + this.randomNum();
        rightSpriteName = "nv" + this.randomNum();
      } else {
        leftSpriteName = "nv" + this.randomNum();
        rightSpriteName = "nan" + this.randomNum();
      }   
  
      name = "roadblock2L+" + this.getNextRoadblockIndex();  
      roadblock.name = name;
      roadblock.parent = this.node;
      
      rb = roadblock.getComponent("roadblock");
      rb.initRoadblock(this, leftSpriteName, middleSpriteName, rightSpriteName, false, 2);
    }
    else
    {
      rb = roadblock.getComponent("roadblock");
      name = roadblock.name;  
    }

    roadblock.setPosition(cc.p(posX, posY));

    rb.resetRoadblockData(score, magicScore, this.speed);  
    rb.running(this.speed);  
    rb.show();

    this.addRoadblocks(name, roadblock);//this.roadblocks.set(name, roadblock);
    return roadblock;
  },
  
  //创建三个人的障碍快
  createRoadblock3: function (posX, posY) {       
    var score = gameInfo.gameRunInfo.baseScoreChange;
    var magicScore = gameInfo.gameRunInfo.baseMagicChange;
    var name = null;  
    var rb = null;  

    var roadblock = this.roadblocksThreeCache.pop();
    if (roadblock == undefined) {
      // cc.error("生成新的双人障碍块");
      roadblock = cc.instantiate(this.roadblockThree);      
      var leftSpriteName = null, middleSpriteName = null, rightSpriteName = null;

      //随机生成三人人物模型图片名称
      var randomTmpLeft = Math.random();
      if (randomTmpLeft < 0.5) {
        leftSpriteName = "nan" + this.randomNum();
      } else {
        leftSpriteName = "nv" + this.randomNum();
      }
      var randomTmpMiddle = Math.random();
      if (randomTmpMiddle < 0.5) {
        middleSpriteName = "nan" + this.randomNum();
      } else {
        middleSpriteName = "nv" + this.randomNum();
      }
      var randomTmpRight = Math.random();
      if (randomTmpRight < 0.5) {
        rightSpriteName = "nan" + this.randomNum();
      } else {
        rightSpriteName = "nv" + this.randomNum();
      } 
  
      name = "roadblock3+" + this.getNextRoadblockIndex();  
      roadblock.name = name;
      roadblock.parent = this.node;

      rb = roadblock.getComponent("roadblock");
      rb.initRoadblock(this, leftSpriteName, middleSpriteName, rightSpriteName, true, 3);
    }
    else
    {
      rb = roadblock.getComponent("roadblock");
      name = roadblock.name;  
    }

    roadblock.setPosition(cc.p(posX, posY));

    rb.resetRoadblockData(score, magicScore, this.speed);  
    rb.running(this.speed);  
    rb.show();

    this.addRoadblocks(name, roadblock);//this.roadblocks.set(name, roadblock);
    return roadblock;
  },

  //增加缓存信息
  addRoadblocks: function (name, roadblock) {      
    this.roadblocks.set(name, roadblock);   
  },
});

